<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, initial-scale=1.0, width=device-width"/>
    <meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
    <title>注册</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/main.css" />
    <link rel="stylesheet" type="text/css" href="../css/login.css" />
</head>
<body style="background-image:url('../image/login.jpg');background-size:100% 100%">
  <div class="login_box" style="margin:10px auto 0 auto;">
      <h1>注册</h1>
      <div class="form-control"><input class="login_input" placeholder="请输入手机号" id="phone"/></div>
      <div class="form-control"><input class="login_input" placeholder="请输入验证码" id="num"/><button class="btn btn-o" onclick="getCode()" id="codeBtn">获取验证码</button></div>
      <!-- <div class="form-control"><input class="login_input" placeholder="请输入您的学号//选填项" id="studentId"/></div> -->
      <div class="form-control"><input class="login_input" placeholder="请输入昵称" id="nickname"/></div>
      <div class="form-control"><input class="login_input"  placeholder="请输入密码(至少6位)" type="password" id="password"/></div>
      <div class="form-control"><input class="login_input" placeholder="请输入邮箱(选填)" id="email"/></div>
      <!-- <div class="form-control"><input class="login_input"  placeholder="再次输入密码(至少6位)" type="password" id="repassword"/></div> -->
      <div class="form-control"><button class="login_btn" onclick="register()" tapmode>注册</button></div>
      <div class="form-control"><a href="login.html"  class="href_white">已有帐号，立刻登录</a></div>
  </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery.js"></script>
<script type="text/javascript" src="../script/main.js"></script>
<script type="text/javascript">
apiready=function(){

}
var phone="",email="",nickname="",num="",password="",systemType="";
function register(){
  var email=$("#email").val();
  //var studentId=$("#studentId").val();
  nickname=$("#nickname").val();
  phone=$("#phone").val();
  num=$("#num").val();
  password=$("#password").val();
  systemType = api.systemType;  // 判断手机是ios还是安卓
  var emailReg=/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
  if(!(/^1[34578]\d{9}$/.test(phone))){
    toast("手机号码有误","bottom");
    return false;
  }
  else if(num==""){
      toast("手机验证码不能为空","bottom");
  }
  else if(nickname == ""){
    toast("昵称不能为空","bottom");
    return false;
  }
  else if(password==""){
      toast("密码不能为空","bottom");
      return false;
  }
  else if(email!==""){
    if(!emailReg.test(email)){
      toast("邮箱格式不正确","bottom")
      return false;
    }else{
      reg();
    }
  }
  else{
      reg();
    }
}
function reg(){
  var data="";
  //注册
  api.ajax({
    url: 'http://47.104.170.122/v1/portal/auth/register/'+num+'?password='+password+'&nickname='+nickname+'&phoneNo='+phone+'&appKey='+systemType+'&email='+email,
    method: 'POST',
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Headers": "Authorization",
      "Content-Type" : 'application/json; charset=utf-8'
    },
    data:{body:data}
  }, function(ret, err) {
    if(ret.code==0){
      api.alert({ msg:'注册成功'});
      openPage('login','login.html');
    }else{
      toast(ret.msg,"bottom");
    }
  });
}
function loop(t,iTime){
  if(t>0){
    t=t-1;
    $("#codeBtn").html(t+'s后可重发');
    $("#codeBtn").attr('disabled','disabled');
    iTime=setTimeout(loop,1000,t,iTime);
  }else{
    clearTimeout(iTime);
    $("#codeBtn").html('重发验证码');
    t=6;
    $("#codeBtn").removeAttr('disabled');
  }
}
function getCode(){
  phone=$("#phone").val();
  if(!(/^1[34578]\d{9}$/.test(phone))){
    toast("手机号码有误","bottom");
    return false;
  }else{
    var t=60;
    var iTime;
    loop(t,iTime);
    //获取验证码接口
    api.ajax({
      url: 'http://47.104.170.122/v1/portal/auth/getRegisterCode?phone='+phone,
      method: 'GET',
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Headers": "Authorization"
      }
    }, function(ret, err) {
      if(ret.code==0){

      }else{
        toast("获取验证码失败，请重新发送",bottom);
      }
    });
  }
}
</script>
</html>
